package cn.zmall.manager.controller;

import cn.zmall.common.pojo.DataTablesResult;
import cn.zmall.common.pojo.Result;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.manager.dto.MemberDto;
import cn.zmall.manager.pojo.TbMember;
import cn.zmall.manager.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @Author BinBini
 * @Date 2020/12/7 16:06
 * @Version 1.0
 */
@RestController
@Api(value = "会员管理")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping(value = "/member/{memberId}", method = RequestMethod.GET)
    @ApiOperation(value = "通过ID获取会员")
    public Result<TbMember> getMemberById(@PathVariable Long memberId) {
        TbMember tbMember = memberService.getMemberById(memberId);
        return new ResultUtil<TbMember>().setData(tbMember);
    }

    @RequestMapping(value = "/member/list", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页搜索排序获取会员列表")
    public DataTablesResult getMemberList(int draw, int start, int length, String searchKey,
                                        String minDate, String maxDate,
                                        @RequestParam("search[value]") String search,
                                        @RequestParam("order[0][column]") int orderCol,
                                        @RequestParam("order[0][dir]") String orderDir) {
        // 获取客户端需要排序的列
        String[] cols = {"checkbox","username", "sex", "phone", "email", "address", "created", "updated", "status"};
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "created";
        }
        // 获取排序方式，默认为desc
        if(orderDir == null) {
            orderDir = "desc";
        }
        if(!search.isEmpty()) {
            searchKey = search;
        }
        DataTablesResult result = memberService.getMemberList(draw, start, length, searchKey, minDate, maxDate, orderColumn, orderDir);
        return result;
    }

    @RequestMapping(value = "/member/removeList", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页搜索排序获取移除会员列表")
    public DataTablesResult getRemoveMemberList(int draw, int start, int lenght, String searchKey,
                                                String minDate, String maxDate,
                                                @RequestParam("search[value]") String search,
                                                @RequestParam("order[0][column]") int orderCol,
                                                @RequestParam("order[0][dir]") String orderDir) {
        // 获取客户端需要排序的列
        String[] cols = {"checkbox","username", "sex", "phone", "email", "address", "created", "updated", "status"};
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "created";
        }
        // 获取排序方式，默认为desc
        if(orderDir == null) {
            orderDir = "desc";
        }
        if(!search.isEmpty()) {
            searchKey = search;
        }
        DataTablesResult result = memberService.getRemoveMemberList(draw, start, lenght, searchKey, minDate, maxDate, orderColumn, orderDir);
        return result;
    }

    @RequestMapping(value = "/member/count", method = RequestMethod.GET)
    @ApiOperation(value = "获得会员总数")
    public DataTablesResult getMemberCount() {
        DataTablesResult result = memberService.getMemberCount();
        return result;
    }

    @RequestMapping(value = "/member/removeCount", method = RequestMethod.GET)
    @ApiOperation(value = "获得移除会员总数")
    public DataTablesResult getRemoveMemberCount() {
        DataTablesResult result = memberService.getRemoveMemberCount();
        return result;
    }

    @RequestMapping(value = "/member/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增会员")
    public Result<TbMember> addMember(@RequestBody MemberDto memberDto) {
        TbMember tbMember = memberService.addMember(memberDto);
        return new ResultUtil<TbMember>().setData(tbMember);
    }

    @RequestMapping(value = "/member/stop/{id}", method = RequestMethod.PUT)
    @ApiOperation(value = "停用会员")
    public Result<TbMember> stopMember(@PathVariable Long id) {
        TbMember tbMember = memberService.alertMemberState(id, 0);
        return new ResultUtil<TbMember>().setData(tbMember);
    }

    @RequestMapping(value = "/member/start/{ids}", method = RequestMethod.PUT)
    @ApiOperation(value = "启用会员")
    public Result<TbMember> startMember(@PathVariable Long[] ids) {
        for(Long id : ids) {
            memberService.alertMemberState(id, 1);
        }
        return new ResultUtil<TbMember>().setData(null);
    }

    @RequestMapping(value = "/member/remove/{ids}", method = RequestMethod.PUT)
    @ApiOperation(value = "移除会员")
    public Result<TbMember> removeMember(@PathVariable Long[] ids) {
        for(Long id : ids) {
            memberService.alertMemberState(id, 2);
        }
        return new ResultUtil<TbMember>().setData(null);
    }

    @RequestMapping(value = "/member/del/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "彻底删除会员")
    public Result<TbMember> delMember(@PathVariable Long[] ids) {
        for(Long id : ids) {
            memberService.deleteMember(id);
        }
        return new ResultUtil<TbMember>().setData(null);
    }

    @RequestMapping(value = "/member/update/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "修改会员信息")
    public Result<TbMember> updateMember(@PathVariable Long id, @ModelAttribute MemberDto memberDto) {
        TbMember tbMember = memberService.updateMember(id, memberDto);
        return new ResultUtil<TbMember>().setData(tbMember);
    }

    @RequestMapping(value = "/member/changePass/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "修改会员密码")
    public Result<TbMember> changeMemberPassword(@PathVariable Long id, @ModelAttribute MemberDto memberDto) {
        TbMember tbMember = memberService.changeMemberPassword(id, memberDto);
        return new ResultUtil<TbMember>().setData(tbMember);
    }

    @RequestMapping(value = "/member/phone", method = RequestMethod.GET)
    @ApiOperation(value = "验证手机号是否存在")
    public Boolean validatePhone(String phone) {
        if(memberService.getMemberByPhone(phone) != null) {
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/member/email", method = RequestMethod.GET)
    @ApiOperation(value = "验证邮箱是否存在")
    public Boolean validateEmail(String email) {
        if(memberService.getMemberByEmail(email) != null) {
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/member/username", method = RequestMethod.GET)
    @ApiOperation(value = "验证用户名是否存在")
    public Boolean validateUsername(String username) {
        if(memberService.getMemberByUsername(username) != null){
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/member/edit/{id}/phone", method = RequestMethod.GET)
    @ApiOperation(value = "验证编辑手机号是否存在")
    public Boolean validateEditPhone(@PathVariable Long id, String phone){
        if(memberService.getMemberByEditPhone(id, phone) != null){
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/member/edit/{id}/email", method = RequestMethod.GET)
    @ApiOperation(value = "验证编辑邮箱是否存在")
    public Boolean validateEditEmail(@PathVariable Long id, String email){
        if(memberService.getMemberByEditEmail(id, email) != null){
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/member/edit/{id}/username", method = RequestMethod.GET)
    @ApiOperation(value = "验证编辑用户名是否存在")
    public Boolean validateEditUsername(@PathVariable Long id, String username){
        if(memberService.getMemberByEditUsername(id, username) != null){
            return false;
        }
        return true;
    }
}
